using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BANSACH.BusinessLayer.DataLayer;
using BANSACH.BusinessLayer;
namespace BANSACH_GUI
{
    public partial class frmTblSACHList : FormBase
    {
        DataView data;
        TblSACHFactory objDao = new TblSACHFactory();
        TblSACH objEnity;
        TblSACHKeys objKeys;
        int idRowSelected = -1;

        public frmTblSACHList()
        {
            InitializeComponent();
        }
        private void frmTblSACHList_Load(object sender, EventArgs e)
        {
            CreateConfigGrid();
            // Load Data
            objDao = new TblSACHFactory();
            objEnity = new TblSACH();
            data = objDao.GetAll().DefaultView;
            //
            grvData.DataSource = data;
        }
        private void CreateConfigGrid()
        {
            grvData.ModeColorRow = 1;
            grvData.SetColorRow(System.Drawing.Color.AliceBlue);

            
				grvData.AddColumns("TextBox","MaSach","Mã sách",200,false,true,"ML","");
				grvData.AddColumns("TextBox","TenSach","Tên sách",250,true,true,"ML","");                
				grvData.AddColumns("TextBox","TacGia","Tác giả",200,true,true,"ML","");
                grvData.Columns["TacGia"].Frozen = true;
				grvData.AddColumns("TextBox","MaNXB","Mã NXB",200,false,true,"ML","");
                grvData.AddColumns("TextBox", "TenNXB", "Tên NXB", 200, true, true, "ML", "");
				grvData.AddColumns("TextBox","NamXB","NamXB",60,true,true,"ML","");
				grvData.AddColumns("TextBox","MaTL","Mã TL",120,false,true,"ML","");
                grvData.AddColumns("TextBox", "TenTL", "Thể loại", 200, true, true, "ML", "");
				grvData.AddColumns("TextBox","TomTat","TomTat",200,false,true,"ML","");
				grvData.AddColumns("TextBox","UrlAnh","UrlAnh",200,false,true,"ML","");
                grvData.AddColumns("TextBox", "GiaBia", "Giá bìa", 120, true, true, "MR", "# ### ###");
                grvData.AddColumns("TextBox", "GiaBan", "Giá bán", 120, true, true, "MR", "# ### ###");
				grvData.AddColumns("TextBox","NgayKK","NgayKK",200,false,true,"ML","");
                grvData.AddColumns("TextBox", "SoluongKK", "SoluongKK", 200, false, true, "MR", "# ### ###");
				grvData.AddColumns("TextBox","Soluong","Số lượng",120,true,true,"MR","# ### ###");
        }
        private void SetDataFilter()
        {
            try
            {
                StringBuilder filter = new StringBuilder("1=1");                
                data.RowFilter = filter.ToString();
            }
            catch
            { }

        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            frmTblSACHDetail frmNew = new frmTblSACHDetail();
            frmNew.OnAddOrEditEntity = new EventHandler(OnInserEntity);
            frmNew.ShowDialog();

        }
        private void OnInserEntity(object sender, EventArgs e)
        { 
            TblSACH objInserted = sender as TblSACH;
            if (objInserted == null) return;            
            DataRowView newrow = data.AddNew();
            // Thêm dòng vào DataView để hiển thị lên Grid
            SetEntityToDataRowView(objInserted,newrow);
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (grvData.SelectedCells.Count == 0) return;
            idRowSelected = grvData.SelectedCells[0].RowIndex;

            objKeys = new TblSACHKeys(grvData["MaSach", idRowSelected].Value.ToString());
            objEnity = objDao.GetByPrimaryKey(objKeys);
            frmTblSACHDetail_Cb frmEdit = new frmTblSACHDetail_Cb(objEnity);
            frmEdit.OnAddOrEditEntity = new EventHandler(OnEditEntity);
            frmEdit.ShowDialog();
        }
        private void OnEditEntity(object sender, EventArgs e)
        {             
            DataRowView edit_Row = data[idRowSelected];
            TblSACH objEdited = sender as TblSACH;
            if (objEdited != null)
                SetEntityToDataRowView(objEdited,edit_Row);
        }
        private void SetEntityToDataRowView(TblSACH obj_Entity,DataRowView _row)
        {
            data.BeginInit();            
            
				 _row["MaSach"] = obj_Entity.MaSach;
				 _row["TenSach"] = obj_Entity.TenSach;
				 _row["TacGia"] = obj_Entity.TacGia;
				 _row["MaNXB"] = obj_Entity.MaNXB;
                 _row["TenNXB"] = obj_Entity.TenNXB;
				 _row["NamXB"] = obj_Entity.NamXB;
				 _row["MaTL"] = obj_Entity.MaTL;
                 _row["TenTL"] = obj_Entity.TenTL;
				 _row["TomTat"] = obj_Entity.TomTat;
				 _row["UrlAnh"] = obj_Entity.UrlAnh;
                 if (obj_Entity.GiaBia!= null)
				    _row["GiaBia"] = obj_Entity.GiaBia;
                 if (obj_Entity.GiaBan!= null)
				    _row["GiaBan"] = obj_Entity.GiaBan;
                 if (obj_Entity.NgayKK!= null)
				    _row["NgayKK"] = obj_Entity.NgayKK;
                 if (obj_Entity.SoluongKK!= null)
				    _row["SoluongKK"] = obj_Entity.SoluongKK;
                 if (obj_Entity.Soluong!= null)
				 _row["Soluong"] = obj_Entity.Soluong;
            data.EndInit();
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (grvData.SelectedCells.Count == 0) return;
            if (MessageBox.Show("Bạn chắc chắn xóa bản ghi này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) != DialogResult.Yes) return;
            idRowSelected = grvData.SelectedCells[0].RowIndex;

            objKeys = new TblSACHKeys(grvData["MaSach", idRowSelected].Value.ToString());
            if (objDao.Delete(objKeys))
                data.Delete(idRowSelected);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            gPanelFind.Visible = !gPanelFind.Visible;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEdit_Click(sender, e);
        }

        private void grvData_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnEdit_Click(sender, e);
            if (e.KeyCode == Keys.Delete)
                btnDelete_Click(sender, e);
        }

        private void btnprint_Click(object sender, EventArgs e)
        {
            //1. lay du lieu
            DataLayerBase objDao = new DataLayerBase();
            // SqlParameter masach = new SqlParameter("@masach", txt_MaSach.Text);
            DataTable data = objDao.Do_SpSelect("sp_thongkedausach");

            //2.khai bao report va dua dl len report
            Reports.rptThongKeSach report = new Reports.rptThongKeSach();

            // truyen tham so
           // report.DataDefinition.FormulaFields["ngaythang"].Text = "'" + FrameWorkSystem.CommonFunction.DateTime2LongString(NgayKK.value) + "'";
            report.DataDefinition.FormulaFields["tieude"].Text = "' THỐNG KÊ SÁCH'";
            report.SetDataSource(data);

            //3. hien thi form
            Reports.frmPreview preview = new Reports.frmPreview();
            preview.Report = report;
            preview.Show();
        } 
      
    }
}
